<?php 
class Article_manager extends Abstract_manager
{
	function __construct()
	{
		parent::__construct();
		$this->table = "articles";
		$this->config = array(
               array(
                     'field'   => 'title', 
                     'label'   => 'Titel', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'fulltext', 
                     'label'   => 'Tekst van artikel', 
                     'rules'   => 'required'
                  )
            );
        $this->form_validation->set_rules($this->config);
        $this->post_data['alias'] = strtolower(url_title($this->input->post("title"), 'dash'));

	}
	function readItems($category_id = false)
	{
		if($category_id)
		{
			return $this->db->get_where($this->table, array("category_id" => $category_id) )->result();
		}
		else 
		{
			return parent::readItems();
		}
	}
	function updateItem($article_id, $new_alias = false)
	{
		parent::updateItem($article_id, $new_alias);
		
		$alias_exist = $this->db->where("alias",  $new_alias)->get("menu_items")->num_rows() > 0;
		if($alias_exist)
				$updated_article_alias = $this->db->get_where($this->table, array("id" => $article_id))->row()->alias;
			 	$this->db->where("alias",  $new_alias);
				$this->db->update("menu_items", array('alias' => $updated_article_alias));
	}
	function makeFrontPage ($old_id, $new_id)
	{
		$this->db->where("id", $old_id);
		$this->db->update($this->table, array("is_frontpage" => 0));
		
		
		$this->db->where("id", $new_id);
		$this->db->update($this->table, array("is_frontpage"=> 1));
		
	}
}

